﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using System.Threading;

namespace md5counter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        protected string GetMD5HashFromFile(string fileName)
        {
            StringBuilder sb;
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open);
            
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();

            sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            }
            catch (Exception ex)
            {
                listBox1.Items.Insert(0,"Error: Could not read file from disk. Original error: " + ex.Message);                
                return "";
            }
            return sb.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {       
            fsTree1.Load();
        }

        private void fsTree1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (File.Exists(e.Node.FullPath))
            {                
                textBox1.Text = GetMD5HashFromFile(e.Node.FullPath);
                if (textBox1.Text != "")
                {
                    label1.Text = "MD5 of a file " + e.Node.FullPath;
                }
                else 
                {
                    label1.Text = "Faild to get md5 of a file: " + e.Node.FullPath;
                }
            }
            else 
            {
                label1.Text = e.Node.Name + " Is not file";
                textBox1.Text = "";
            }
        }
        private void checkchildren(string Node,string Mode,ListBox l) 
        {
            if (Directory.Exists(Node))
            {
                if (Mode == "check")
                {
                    foreach (string d in Directory.GetFiles(Node))
                    {
                        if (false == l.Items.Contains(d))
                        {
                            l.Items.Add(d);
                        }
                    }
                    foreach (string d in Directory.GetDirectories(Node))
                    {
                        checkchildren(d, Mode,l);
                    }
                }
                else
                {
                    foreach (string d in Directory.GetFiles(Node))
                    {
                        l.Items.Remove(d);
                    }
                    foreach (string d in Directory.GetDirectories(Node))
                    {
                        checkchildren(d, Mode,l);
                    }
                }

            }
        }

        private void fsTree1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            Thread t = new Thread(delegate(){ Go(e,listBox2); });
            t.Start(); 
            //if (e.Node.Checked == true)
            //{
            //    if (e.Node.Nodes.Count == 0 & File.Exists(e.Node.FullPath))
            //    {
            //        listBox2.Items.Add(e.Node.FullPath);
            //    }
               
            //        checkchildren(e.Node.FullPath, "check");
                
            //}
            //else 
            //{
            //    checkchildren(e.Node.FullPath, "uncheck");
            //    listBox2.Items.Remove(e.Node.FullPath);
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox3.Items.Clear();
            foreach (string t in listBox2.Items)
            {
                listBox3.Items.Add(GetMD5HashFromFile(t));
            }
        }
        void Go(TreeViewEventArgs e,ListBox l)
        {
            if (e.Node.Checked == true)
            {
                if (e.Node.Nodes.Count == 0 & File.Exists(e.Node.FullPath))
                {
                    l.Items.Add(e.Node.FullPath);
                }
               
                    checkchildren(e.Node.FullPath, "check",l);
                
            }
            else 
            {
                checkchildren(e.Node.FullPath, "uncheck",l);
                l.Items.Remove(e.Node.FullPath);
            }
        }  
    }
}
